from spider import CommentSpider, RandomMovieSpider
from analysis import Painter
import pandas as pd
import re
import os


def apply_one_movie(key_word):
    movie_name = CommentSpider.crawl_comment_as_csv(search_word=key_word)  # 输入待分析的电影名称
    high_csv = 'dataset/' + movie_name + '_好评.csv'
    mid_csv = 'dataset/' + movie_name + '_中评.csv'
    low_csv = 'dataset/' + movie_name + '_差评.csv'

    high_data = pd.read_csv(high_csv, delimiter=',')
    mid_data = pd.read_csv(mid_csv, delimiter=',')
    low_data = pd.read_csv(low_csv, delimiter=',')

    concat_pd = pd.concat([high_data, mid_data, low_data], axis=0, join='inner')
    Painter.dashboard1_page(concat_pd)

def apply_random_movie(page=10, seed=9527):
    total = RandomMovieSpider.crawl_data_as_csv(page + 2, seed)
    # total2 = RandomMovieSpider.crawl_data_as_csv2(page + 2, seed)
    # total = total1 + total2
    filename = 'dataset/' + str(page + 2) + '_' + str(seed) + '电影数据.csv'
    # filename2 = 'dataset/' + str(page + 2) + '_' + str(seed) + '电影数据.csv'
    # add_csv(filename, filename2)
    data = pd.read_csv(filename, delimiter=',')
    genre_list = get_genre_list(data)
    country_list = get_country_list(data)
    sort_data = data.sort_values('评分', ascending=False)
    time_line, time_dict = get_time_line_dict(data)
    Painter.dashboard2_page(genre_list, country_list, sort_data, time_line, time_dict, total)

def add_csv(filename, filename2):
    df = pd.read_csv(filename2, encoding="utf8")
    df.to_csv(filename, encoding="utf8", index=False, header=True, mode='a')  # 如果对每个文件的标题另外有需求，需要对header变量进行指定
    df = pd.read_csv(filename, header=0)
    datalist = df.drop_duplicates()
    datalist.to_csv(filename)

def get_time_line_dict(data):
    sort_by_time = data.sort_values('上映时间')
    sort_time_list = sort_by_time['上映时间'].to_list()
    cut_time_list = [item.strip()[:7] if re.match(r'\d+-\d+', item.strip()[:7]) else 'Nan' for item in sort_time_list]
    time_line = list(set(cut_time_list))
    time_line.sort()
    time_dict = {}
    for times in time_line:
        time_dict[times] = {}
        time_dict[times]['movie'] = []
        time_dict[times]['score'] = []
    for name, times, score in zip(sort_by_time['电影名称'].to_list(), cut_time_list, sort_by_time['评分'].to_list()):
        if times == 'Nan':
            continue
        time_dict[times]['movie'].append(name)
        time_dict[times]['score'].append(score)
    return time_line, time_dict


def get_genre_list(data):
    genre = data['剧情类型'].value_counts()
    genre_dict = genre.to_dict()
    new_genre_set = set()
    for k in genre_dict.keys():
        for item in k.split("/"):
            new_genre_set.add(item)
    new_genre_dict = {}
    for genre in new_genre_set:
        new_genre_dict[genre] = 0

    for k, v in genre_dict.items():
        for item in k.split("/"):
            new_genre_dict[item] += int(v)
    return sorted(new_genre_dict.items(), key=lambda x: x[1])[::-1]

def get_country_list(data):
    country = data['制片国家'].value_counts()
    country_dict = country.to_dict()
    new_country_set = set()
    for k in country_dict.keys():
        for item in k.split("/"):
            new_country_set.add(item)
    new_country_dict = {}
    for genre in new_country_set:
        new_country_dict[genre] = 0

    for k, v in country_dict.items():
        for item in k.split("/"):
            new_country_dict[item] += int(v)
    return sorted(new_country_dict.items(), key=lambda x: x[1])[::-1]

def shell():
    print("----------欢迎来到豆瓣电影分析系统----------")
    while True:
        print("=" * 30)
        print("【选择1】 单部电影分析\n【选择2】 随机电影分析\n 【q】退出")
        print("=" * 30)
        choice = input("请输入您的选择>>>")
        if 'q' in choice:
            print("退出成功")
            break
        if eval(choice) == 1:
            movie_name = input("请输入电影名称>>>")
            apply_one_movie(movie_name)
        elif eval(choice) == 2:
            page = int(input("请输入随机爬取页数>>>"))
            apply_random_movie(page)
        else:
            continue


if __name__ == '__main__':
    shell()
